local temp = {}
local stock = 0
local old = 0
local res = {}
local jsonTemp = {}
for i=1, #KEYS do
	jsonTemp = {}
	old = redis.call("HGET", KEYS[i], "stock")
	stock = redis.call("HINCRBY", KEYS[i], "stock", -ARGV[i])
	redis.call("HINCRBY", KEYS[i], "sales", ARGV[i])
	temp[i] = KEYS[i]
	
	jsonTemp["key"] = KEYS[i]
	jsonTemp["old"] = old
	jsonTemp["stock"] = stock
	jsonTemp["num"] = ARGV[i]
	res[i] = jsonTemp
	if stock < 0 then
		break
	end
end

if stock < 0 then
	for i=1, #temp do
		redis.call("HINCRBY", temp[i], "stock", ARGV[i])
		redis.call("HINCRBY", temp[i], "sales", -ARGV[i])
	end
	return "NO"
else
	return cjson.encode(res)
end
